import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:github_client_app/bloc/counter2_cubit.dart';
import 'package:github_client_app/bloc/counter_bloc.dart';
import 'package:github_client_app/bloc/counter_cubit.dart';
import 'package:github_client_app/bloc/simple_bloc_observer.dart';

main(List<String> args) async {
  // cubit
  // 顺序，想执行SimpleBlocObserver中的onchange，再执行内部的onchange
  // 改变onchange中的值，不会影响state
  // Bloc.observer = SimpleBlocObserver();

  // var count = CounterCubit();

  // // var listener = count.stream.listen(print);
  // count.increment();

  // Future.delayed(Durations.extralong1, () {
  //   var count2 = Counter2Cubit();

  //   count2.increment();
  //   print(count2.state);
  // });

  // print(count.state);

  // count.close();
  // listener.cancel();

  // bloc
  // Bloc.observer = SimpleBlocObserver();

  // final bloc = CounterBloc();
  // // final subscription = bloc.stream.listen(print); // 1
  // bloc.add(CounterIncrementPressed());
  // await Future.delayed(Duration.zero);
  // await subscription.cancel();
  // await bloc.close();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) {
        return CounterBloc();
      },
      child: const MaterialApp(
        title: "Flutter project",
        home: MyHome(),
      ),
    );
  }
}

class MyHome extends StatelessWidget {
  const MyHome({super.key});

  @override
  Widget build(BuildContext context) {
    print("object");
    return Scaffold(
      appBar: AppBar(title: const Text("ddddd")),
      body: Column(
        children: [
          BlocBuilder<CounterBloc, int>(
            buildWhen: (previous, current) {
              return true;
            },
            builder: (context, count) {
              return Text("data+$count");
            },
          ),
          ElevatedButton(
            onPressed: () {
              context.read<CounterBloc>().add(CounterIncrementPressed());
            },
            child: const Text('+1'),
          ),
          ElevatedButton(
            onPressed: () {
              context.read<CounterBloc>().add(CounterDecrementPressed());
            },
            child: const Text('-1'),
          ),
        ],
      ),
    );
  }
}
